import http from '@/utils/http-util'
import ciphertext from '@/utils/ciphertext-util'

let BASE_USER_CONTENT = process.env.WEB_CONTEXT_PATH + 'sys/user'
/** 获取用户 **/
export function getUserInfoApi() {
  return http({
    url: BASE_USER_CONTENT + '/info',
    method: 'get',
    params: http.queryParam()
  })
}

/** 修改密码 **/
export function updatePasswordApi(dataForm) {
  return http({
    url: BASE_USER_CONTENT + '/password',
    method: 'put',
    data: http.bodyData({
      'password': ciphertext.encode(dataForm.oldPassword),
      'newPassword': ciphertext.encode(dataForm.newPassword)
    })
  })
}

/** 获取用户列表 **/
export function getUserListApi(data = {}) {
  return http({
    url: BASE_USER_CONTENT + '/list',
    method: 'post',
    data: http.bodyData({
      'page': data.page || 1,
      'pageSize': data.pageSize || 20,
      'sort': {
        'field': data.sortField,
        'rule': data.sortRule === undefined ? 1 : data.sortRule
      },
      'minCreateTime': data.minCreateTime || '',
      'maxCreateTime': data.maxCreateTime || '',
      'status': data.status || '',
      'userName': data.userName || ''
    })
  })
}

/** 删除用户 **/
export function deleteUserApi(data = {}) {
  return http({
    url: BASE_USER_CONTENT + '/' + data.id,
    method: 'delete',
    params: http.queryParam()
  })
}

/** 批量删除用户 **/
export function batchDeleteUserApi(data = []) {
  return http({
    url: BASE_USER_CONTENT + '/deletes',
    method: 'delete',
    data: http.bodyData({
      ids: data
    })
  })
}

/** 获取用户信息 **/
export function getUserApi(userId) {
  return http({
    url: BASE_USER_CONTENT + '/' + userId,
    method: 'get',
    params: http.queryParam(),
    loading: false
  })
}

/** 新增用户信息 **/
export function saveUserApi(data = {}) {
  return http({
    url: BASE_USER_CONTENT + '/' + 'save',
    method: 'post',
    data: http.bodyData({
      userName: data.userName,
      nickName: data.nickName,
      password: ciphertext.encode(data.password),
      email: data.email,
      status: data.status,
      expired: data.expired,
      roleIds: data.roleIds
    }),
    loading: false
  })
}

/** 重置用户密码 **/
export function restUserPasswordApi(userId) {
  return http({
    url: BASE_USER_CONTENT + '/' + userId + '/restPassword',
    method: 'put',
    params: http.queryParam(),
    loading: false
  })
}

/** 修改用户信息 **/
export function updateUserApi(data = {}) {
  return http({
    url: BASE_USER_CONTENT + '/' + 'update',
    method: 'put',
    data: http.bodyData({
      id: data.id || 0,
      userName: data.userName,
      password: ciphertext.encode(data.password),
      status: data.status,
      expired: data.expired,
      roleIds: data.roleIds || []
    }),
    loading: false
  })
}
